package top.yisen614.sharedelement

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import android.widget.ImageView
import android.widget.TextView
import androidx.core.app.ActivityCompat
import androidx.core.app.ActivityOptionsCompat
import androidx.core.util.Pair
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import top.yisen614.sharedelement.util.Image


/**
 * created by 16539
 * 2019/12/11
 */
class RecyclerAdapter(private val images: MutableList<Image>) :
    RecyclerView.Adapter<RecyclerAdapter.RecyclerHolder>() {


    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerHolder {
        val view = LayoutInflater.from(parent.context)
            .inflate(R.layout.main_recycler_layout, parent, false)
        return RecyclerHolder(view)
    }

    override fun getItemCount(): Int {
        return images.size
    }

    override fun onBindViewHolder(holder: RecyclerHolder, position: Int) {
        holder.hold(images[position])
    }

    class RecyclerHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        private var imageView: ImageView = itemView.findViewById(R.id.main_recycler_image)
        private var textView: TextView = itemView.findViewById(R.id.main_recycler_title)

        fun hold(image: Image) {

            Glide.with(itemView.context).load(image.url).into(imageView)
            textView.text = image.title

            val anim = ScaleAnimation(
                0.95f,
                1.0f,
                0.95f,
                1.0f,
                Animation.RELATIVE_TO_SELF,
                0.5f,
                Animation.RELATIVE_TO_SELF,
                0.5f
            )

            anim.duration = 200//to make duration random number between [0,501)
            itemView.startAnimation(anim)

            itemView.setOnClickListener { view ->
                val intent = Intent(view.context, DetailActivity::class.java)

                val bundle = Bundle()

                bundle.putString(DetailActivity.IMAGE_URL, image.url)
                bundle.putString(DetailActivity.IMAGE_TITLE, image.title)

                intent.putExtras(bundle)

                val actionOptions = ActivityOptionsCompat.makeSceneTransitionAnimation(
                    itemView.context as Activity,
                    Pair(
                        itemView.findViewById(R.id.main_recycler_image),
                        DetailActivity.DETAIL_IMAGE_NAME
                    ),
                    Pair(
                        itemView.findViewById(R.id.main_recycler_title),
                        DetailActivity.DETAIL_TEXT_NAME
                    )
                )

                ActivityCompat.startActivity(itemView.context, intent, actionOptions.toBundle())
            }
        }
    }
}